
"""
    练习网址在本机的文件，http://localhost:8080/WebTest_war_exploded/pages/test.html
    需要在javaIDE中启动这个html用tomcat
"""
import time

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument("--incognito")  # 启用无痕模式
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
driver.get("http://localhost:8080/WebTest_war_exploded/pages/test.html")
# time.sleep(5)
driver.implicitly_wait(20)
"""
    点击警告弹窗
"""
#点击Alert按钮
driver.find_element(By.XPATH,'/html/body/div/div[1]/button[1]').click()
time.sleep(0.5)
#点击确定
driver.switch_to.alert.accept()
time.sleep(0.5)

#点击Confirm按钮
driver.find_element(By.XPATH,'/html/body/div/div[1]/button[2]').click()
time.sleep(0.5)
#点击确定/取消 的确定
driver.switch_to.alert.accept()
time.sleep(0.5)
#点击确定
driver.switch_to.alert.accept()
time.sleep(0.5)
#点击Confirm按钮
driver.find_element(By.XPATH,'/html/body/div/div[1]/button[2]').click()
time.sleep(0.5)
#点击确定/取消 的取消
driver.switch_to.alert.dismiss()
time.sleep(0.5)
#点击确定
driver.switch_to.alert.accept()
time.sleep(0.5)

#点击Prompt按钮
driver.find_element(By.XPATH,'/html/body/div/div[1]/button[3]').click()
time.sleep(0.5)
#输入文本：哈哈哈
driver.switch_to.alert.send_keys("hhh")
time.sleep(0.5)
#点击确定
driver.switch_to.alert.accept()
#再点击确定
driver.switch_to.alert.accept()
#退出弹窗
time.sleep(0.5)
"""
    点击单选框选择男
"""
driver.find_element(By.XPATH,'//*[@id="male"]').click()
"""
    点击多选框全部选择
"""
time.sleep(1)
driver.find_element(By.XPATH,'//*[@id="sports"]').click()

driver.find_element(By.XPATH,'//*[@id="music"]').click()

driver.find_element(By.XPATH,'//*[@id="reading"]').click()


"""
    在frame框架中输入Java点击搜索
"""
#进入frame
driver.switch_to.frame(driver.find_element(By.XPATH,'//*[@id="testFrame"]'))
# 找到搜索框,输入java,按下回车
# driver.find_element(By.XPATH,'//*[@id="kw"]').send_keys("java",Keys.ENTER)
search_box = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="kw"]'))
)
print("进入iframe")
# 再执行 send_keys 操作
search_box.send_keys("java", Keys.ENTER)
# 点击搜索
# driver.find_element(By.XPATH,'//*[@id="su"]').click()
#按回车键搜索
#退出到frame的外层
# driver.switch_to.parent_frame()
#退出到最外层
driver.switch_to.default_content()
print("搜索完成")
time.sleep(1)

"""
    在悬停按钮上悬停点击跳转到哔哩哔哩
"""
#找到需要悬停的元素
element1 = driver.find_element(By.XPATH,'/html/body/div/div[5]/div')
#创建ActionChains对象 ，接收driver
actions = ActionChains(driver)
#调用对象的方法传入参数，最后用perform执行操作
actions.move_to_element(element1).perform()
#悬停后出现的元素可以直接使用driver的find_element()查找元素与普通页面上的元素一样
#获取句柄，便于后面返回本页面
handle1 = driver.current_window_handle
element2 = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'/html/body/div/div[5]/div/span/p[1]/a')))
#点击悬停后出现的元素，跳转到哔哩哔哩
element2.click()
#返回原来的页面操作
driver.switch_to.window(handle1)

time.sleep(1)
"""
    在基本下拉框中选择‘选项二’
"""
select_element = driver.find_element(By.XPATH,'//*[@id="basicSelect"]')
select = Select(select_element)
select.select_by_visible_text('选项二')
# #点击下拉框
# driver.find_element(By.XPATH,'//*[@id="basicSelect"]').click()
# #点击选项三
# driver.find_element(By.XPATH,'//*[@id="basicSelect"]/option[3]').click()
time.sleep(1)
"""
    在多级联动下拉列表中选择‘广东省’‘深圳市’
"""
#定位下拉框
select_element = driver.find_element(By.XPATH,'//*[@id="province"]')
#创建Select对象（）
select = Select(select_element)
# 通过对象的选择文本方式确定要选择的内容
select.select_by_visible_text('北京市')

#定位下拉框
select_element = driver.find_element(By.XPATH,'//*[@id="city"]')
#创建Select对象（）
select = Select(select_element)
# 通过对象的选择文本方式确定要选择的内容
select.select_by_visible_text('北京市')
time.sleep(3)
print("一切正常")
driver.get_screenshot_as_file(r'C:\Users\suyujie\Desktop\浏览器屏幕截图.png',)
driver.quit()


